<template>
  <el-dialog :title="title" :visible="show" @close="addEvent">
    <el-form ref="from" label-width="80px" :model="from" :rules="rules">
      <el-form-item label="角色名" prop="name">
        <el-input v-model="from.name" />
      </el-form-item>
      <el-form-item label="描述信息" prop="description">
        <el-input v-model="from.description" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="addEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysRolePOSTAPI, sysRolPUTAPI } from '@/api'
export default {
  model: {
    prop: 'show',
    event: 'input'
  },
  props: ['show'],
  data() {
    return {
      //   isShow: false
      from: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 10, message: '请输入1-10个字符', trigger: 'change' }
        ],
        description: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 100, message: '请输入1-100个字符', trigger: 'change' }
        ]
      },
      mode: 'add'
    }
  },
  computed: {
    title() {
      return { add: '新增角色', edit: '编辑角色' }[this.mode]
    }
  },
  methods: {
    addEvent() {
      this.$emit('input', false)
      this.$refs.from.resetFields()
      this.from = {
        name: '',
        description: ''
      }
    },
    async submit() {
      if (this.mode == 'add') { 
        this.$refs.from.validate(async(result) => {
          if (result) {
            await sysRolePOSTAPI(this.from)
            this.$message.success('添加成功')
          }
        })
      } else if (this.mode == 'edit') {
        await sysRolPUTAPI(this.from)
        this.$message.success('编辑成功')
      }
      this.addEvent()
      this.$emit('getSysRoleAPI')
    }
  }
}
</script>

<style></style>
